package com.cacshanghai.cac_oa_client.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cacshanghai.cac_oa_client.R;
import com.cacshanghai.cac_oa_client.adapter.CalendarAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by geyh on 2016/6/2.
 */
public class DailyCalendarFragment extends Fragment {

    private String currentDate = "";
    private int year_c = 0;
    private int month_c = 0;
    private int day_c = 0;
    private int jumpMonth = 0; // 每次滑动，增加或减去一个月,默认为0（即显示当前月）
    private int jumpYear = 0; // 滑动跨越一年，则增加或者减去一年,默认为0(即当前年)
    private TextView currentMonth;
    private ImageView prevMonth;
    private ImageView nextMonth;
    private GridView gv_calendar;
    private CalendarAdapter calendarAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_calendar_daily, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initCalendar();
    }

    private void initCalendar() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        currentDate = sdf.format(date); // 当期日期
        year_c = Integer.parseInt(currentDate.split("-")[0]);
        month_c = Integer.parseInt(currentDate.split("-")[1]);
        day_c = Integer.parseInt(currentDate.split("-")[2]);

        currentMonth = (TextView) getView().findViewById(R.id.currentMonth);
        prevMonth = (ImageView) getView().findViewById(R.id.prevMonth);
        prevMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prevMonth();
            }
        });
        nextMonth = (ImageView) getView().findViewById(R.id.nextMonth);
        nextMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextMonth();
            }
        });
        gv_calendar = (GridView) getView().findViewById(R.id.gv_calendar);
        calendarAdapter = new CalendarAdapter(getContext(), getActivity().getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
        gv_calendar.setAdapter(calendarAdapter);
        changeCalendarTitle();
        gv_calendar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //转换点击的date
                String year = calendarAdapter.getShowYear();
                String month = calendarAdapter.getShowMonth();
                String day = calendarAdapter.getDateByClickItem(position).split("\\.")[0];
                Date date = null;
                try {
                    date = new SimpleDateFormat("yyyyMd").parse(year + month + day);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                ((DailyActivity) getActivity()).verticalViewPager.setCurrentItem(1);

            }
        });
    }

    private void nextMonth() {
        jumpMonth++;
        calendarAdapter = new CalendarAdapter(getContext(), this.getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
        gv_calendar.setAdapter(calendarAdapter);
        changeCalendarTitle();
    }

    private void prevMonth() {
        jumpMonth--;
        calendarAdapter = new CalendarAdapter(getContext(), this.getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
        gv_calendar.setAdapter(calendarAdapter);
        changeCalendarTitle();
    }

    private void changeCalendarTitle() {
        StringBuffer textDate = new StringBuffer();
        textDate.append(calendarAdapter.getShowYear()).append("年").append(calendarAdapter.getShowMonth()).append("月").append("\t");
        currentMonth.setText(textDate);
    }
}
